package com.vteba.security.meta;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.util.Assert;

import com.vteba.common.constant.CommonConst;
import com.vteba.security.user.Authority;

public class DefaultGrantedAuthority implements GrantedAuthority {
	private static final long serialVersionUID = 1205440927723986197L;

	private final String role;
	
	private String authName;
	private String tenantId;

	public DefaultGrantedAuthority(Authority authority) {
		this(authority.getAuthName(), authority.getTenantId());
	}
	
	public DefaultGrantedAuthority(String authName, String tenantId) {
		Assert.hasText(authName, "A granted authority authName is required");
		if (tenantId != null) {
			this.role = authName + CommonConst.SHARP + tenantId;
		} else {
			this.role = authName;
		}
		this.authName = authName;
		this.tenantId = tenantId;
	}
	
	public DefaultGrantedAuthority(String role) {
		Assert.hasText(role, "A granted authority textual is required");
		this.role = role;
	}

	public String getAuthority() {
		return role;
	}

	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}

		if (obj instanceof DefaultGrantedAuthority) {
			return role.equals(((DefaultGrantedAuthority) obj).role);
		}

		return false;
	}

	public int hashCode() {
		return this.role.hashCode();
	}

	public String toString() {
		return this.role;
	}

	public String getAuthName() {
		return authName;
	}

	public void setAuthName(String authName) {
		this.authName = authName;
	}

	public String getTenantId() {
		return tenantId;
	}

	public void setTenantId(String tenantId) {
		this.tenantId = tenantId;
	}
}
